import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPool{
    //n表示有多少个线程

    private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<>(1000);

    public MyThreadPool(int n) {
        for (int i = 0; i < n; i++) {

            Thread t = new Thread(() -> {
                while (true) {  //循环取出工作列表的任务
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();  //执行任务
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);  //将任务放入工作列表

    }

}


public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool=new MyThreadPool(4);
        for (int i = 0; i < 1000; i++) {
            int id=i;
            pool.submit(()->{
                System.out.println("执行任务"+id+","+Thread.currentThread().getName() );
            });
        }
    }
}
